package com.a.ims.controller;

import com.a.ims.entity.User;
import com.a.ims.entity.Warehouse;
import com.a.ims.service.AdminlogService;
import com.a.ims.service.UserService;
import com.a.ims.service.WarehouseService;
import com.a.ims.util.R;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/warehouse")
public class WarehouseController {

    @Autowired
    private WarehouseService warehouseService;
    @Autowired
    private AdminlogService adminlogService;

    //记录日志
    private void log(HttpSession session, String log) {
        User user = (User) session.getAttribute("user");
        adminlogService.log(user.getAdminId(), log);
    }


    @GetMapping("/list")
    public R<List<Warehouse>> getAll(HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return R.error("用户未登录");
        }
        List<Warehouse> list = warehouseService.getAllWarehouses();
        return R.ok(list);
    }

    @PostMapping("/add")
    public R<Warehouse> add(@RequestBody Warehouse warehouse, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return R.error("用户未登录");
        }
        if (user.getPermission() > 1) {
            int result = warehouseService.addWarehouse(warehouse);
            if(result == 0) {
                //添加失败
                System.out.println("添加失败");
                return R.error("添加失败");
            } else {
                //添加成功
                log(session, "添加仓库：" + warehouse);
                return R.ok(warehouse);
            }
        }
        return R.error("权限不足");
    }

    @PostMapping("/update")
    public R<Warehouse> update(@RequestBody Warehouse warehouse, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return R.error("用户未登录");
        }
        if (user.getPermission() > 1) {
            int result = warehouseService.modifyWarehouse(warehouse);
            if(result == 0) {
                //修改失败
                System.out.println("修改失败");
                return R.error("修改失败");
            } else {
                //修改成功
                log(session, "修改仓库：" + warehouse);
                return R.ok(warehouse);
            }
        }
        return R.error("权限不足");
    }

    @PostMapping("/delete/{name}")
    public R<String> delete(@PathVariable String name, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return R.error("用户未登录");
        }
        if (user.getPermission() > 1) {
            int result = warehouseService.removeWarehouse(name);
            if(result == 0) {
                //删除失败
                System.out.println("删除失败");
                return R.error("删除失败");
            } else {
                //删除成功
                log(session, "删除仓库：" + name);
                return R.ok("删除成功");
            }
        }
        return R.error("权限不足");
    }
}